package com.tmp.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.pvo.cfg.ColMatch;
import com.pvo.cfg.Pvocfg;
import com.pvo.cfg.TypeMatch;
import com.pvo.cfg.TypeMatchesConfig;
import com.pvo.vo.ColumnVO;
import com.pvo.vo.EntityVO;
import com.pvo.vo.FieldVO;
import com.pvo.vo.TableVO;

public class EntityDTOUtil {
	
	/** 值类型：数值 */
	private final static String VALUE_TYPE_NUM = "num";
	/** 值类型：日期 */
	private final static String VALUE_TYPE_DATE = "date";
	/** 值类型：字符串 */
	
	public static EntityVO getEntityVO(TableVO tableVO, Pvocfg pvocfg) {
		if (tableVO == null) {
			return null;
		}
		EntityVO entityVO = new EntityVO();
		if (pvocfg != null && StringUtils.isNotEmpty(pvocfg.getEntityName()) ) {
			entityVO.setName(pvocfg.getEntityName());
		} else {
			if (StringUtils.isNotEmpty(tableVO.getName())) {
				entityVO.setName(PvoUtil.getEntityNameByTableName(tableVO.getName()));
			}
		}
		entityVO.setComment(tableVO.getComment());
		List<ColumnVO> columnList = tableVO.getColumnList();
		List<FieldVO> fieldList = new ArrayList<FieldVO>();
		if (columnList != null && columnList.size() > 0) {
			for (ColumnVO columnVO : columnList) {
				FieldVO fieldVO = new FieldVO();
				String filedName = PvoUtil.getFieldNameByColumnName(columnVO.getName());
				String comment = columnVO.getComment();
				fieldVO.setFieldName(filedName);
				fieldVO.setComment(comment);
				fieldVO.setColumnVO(columnVO);
				fieldVO.setPrimaryKey(columnVO.isPrimaryKey());
				fieldList.add(fieldVO);
			}
			entityVO.setFieldList(fieldList);
		}
		setFieldInfo(entityVO);
		return entityVO;
	}
	
	private  static void setFieldInfo(EntityVO entityVO) {
		if (entityVO == null) {
			return;
		}
		
		TypeMatch typeMatch = TypeMatchesConfig.getTypeMatch(DynamicConstant.DB_TYPE);
		if (typeMatch == null) {
			throw new RuntimeException("not exists db typematch info : " + DynamicConstant.DB_TYPE);
		}
		List<FieldVO> fieldList = entityVO.getFieldList();
		if (fieldList == null || fieldList.size() <= 0) {
			return;
		}
		// 字段类型匹配信息
		List<ColMatch> colMatchList = typeMatch.getMatchs();
		Map<String, ColMatch> map = new HashMap<String, ColMatch>();
		if (colMatchList != null && colMatchList.size() > 0) {
			for (ColMatch colMatch : colMatchList) {
				map.put(colMatch.getColType().trim().toLowerCase(), colMatch);
			}
		}
		for (FieldVO fieldVO : fieldList) {
			ColumnVO columnVO = fieldVO.getColumnVO();
			String colType = columnVO.getType();
			if (map.containsKey(colType.trim().toLowerCase())) {
				ColMatch colMatch = map.get(colType.trim().toLowerCase());
				String fieldType = colMatch.getFiledType();
				if (VALUE_TYPE_NUM.equalsIgnoreCase(colMatch.getValueType())) {
					if (columnVO.getDecimalDigits() <= 0) {
						fieldType = Long.class.getName();
					} else {
						fieldType = Double.class.getName();
					}
				} else if (VALUE_TYPE_DATE.equalsIgnoreCase(colMatch.getValueType())) {
					fieldVO.setTimestamp(true);
				}
				fieldVO.setType(fieldType);
			} else {
				throw new RuntimeException(DynamicConstant.DB_TYPE + " error column type : " + colType);
			}
		}
		
	}
	

	
	public static void main(String[] args) {
		
	}
}
